package com.i2f.securityKeyboard;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.i2f.i2msk.Security;


public class SecurityKeyboardServlet extends HttpServlet {

	private static final long serialVersionUID = -1533576084616024821L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setContentType("application/octet-stream");
		resp.setHeader("Pragma", "No-cache");
		resp.setDateHeader("Expires", 1L);
		resp.setHeader("Cache-Control", "no-cache");
		String pwd=req.getParameter("pwd0");
		String pwd1=req.getParameter("pwd1");
		if(pwd!=null){
			String[] p=Security.decoderPwd(pwd,pwd1);
			resp.getWriter().print(p[0]+"\t\n"+p[1]);
			resp.getWriter().flush();
			
			return;
		}
		String keypad = Security.keyboardFactory(req.getParameter("type"),req.getParameter("method"),req.getParameter("v"));
//		keypad= keypad.replaceAll("\"", "\\\\\"");
//		keypad= keypad.replaceAll("\\{", "\\{");
//		keypad= keypad.replaceAll("\\}", "\\}");
//		keypad= keypad.replaceAll("\\[", "\\[");
//		keypad= keypad.replaceAll("\\]", "\\]");
//		String s="{\"result\":\""+keypad+"\"}";
//		resp.getOutputStream().write(s.getBytes());
		
		JSONObject o = new JSONObject();
		o.put("result", keypad);
		resp.getOutputStream().write(o.toJSONString().getBytes());
		resp.getOutputStream().flush();
		
	}
	
	
}
